<?php
namespace Admin\Models\ims;
use Admin\Models\BaseModel;
use Helpers\Cache;

class XHImsProduct extends BaseModel
{

    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(type="integer", length=11, nullable=false)
     */
    public $iProdId;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iTypeId;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=true)
     */
    public $sMeasures;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=true)
     */
    public $sProdName;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iDefNum;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $bDelete;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tCreateDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tLastModifyDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iLastModifyUserId;

    /**
     * 采购价
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     *
     * create by dale 20180424
     */
    public $iInPrice;

    /**
     * 出售价
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     *
     *  create by dale 20180424
     */
    public $iOutPrice;

    /**
     * 品牌
     *
     * @var string
     * create by xjq 20180511
     */
    public $sBrand;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSource("XHIms_Product");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHIms_Product';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XHImsProduct[]|XHImsProduct|\Phalcon\Mvc\Model\ResultSetInterface
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XHImsProduct|\Phalcon\Mvc\Model\ResultInterface
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    public static function getProdEnum(){
        if(Cache::exists('prodEnum')){
            return Cache::get('prodEnum');
        }
        $prods=self::find([
            'columns'   => 'iProdId,sProdName',
            'conditions' => 'bDelete=0',
        ])->toArray();
        $array=array_column($prods,'sProdName','iProdId');
        Cache::save('prodEnum',$array);
        return $array;
    }

    public static function getProdType(){
        $prods=self::find([
            'columns'   => 'iProdId,iTypeId',
            'conditions' => 'bDelete=0',
        ])->toArray();
        $array=array_column($prods,'iTypeId','iProdId');
        return $array;
    }

    public static function getProdTypeById($prodId){
        $prodType = self::getProdType();
        return $prodType[$prodId];
    }

}
